package com.qianfeng.controller;

import com.qianfeng.dto.Message;
import com.qianfeng.dto.Page;
import com.qianfeng.dto.Result;
import com.qianfeng.entity.Attendance;
import com.qianfeng.enums.CommonEnum;
import com.qianfeng.exceptions.CommonException;
import com.qianfeng.service.AttendanceService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;

    /**
     * 添加
     *
     * @param attendance
     * @return
     */
    @RequiresRoles(value = "manager")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/add_attendance")
    public Result addAttendance(Attendance attendance) {
        try {
            Result result = new Result();
            attendanceService.addAttendance(attendance);
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.ADD_ATTENDANCE_ERROR);
        }
    }

    /**
     * 删除
     *
     * @param attendanceId
     * @return
     */
    @RequiresRoles(value = "manager")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/delete_attendance")
    public Result deleteAttendance(Integer attendanceId) {
        try {
            Result result = new Result();
            attendanceService.deleteAttendance(attendanceId);
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.DELETE_ATTENDANCE_ERROR);
        }
    }

    @RequiresRoles(value = "manager")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/update_attendance")
    public Result updateAttendance(Attendance attendance) {
        try {
            Result result = new Result();
            attendanceService.updateAttendance(attendance);
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.UPDATE_ATTENDANCE_ERROR);
        }
    }

    /**
     * 分页
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequiresRoles(value = "manager")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/getAttendancePage")
    public Result getAttendancePage(Integer pageNo, Integer pageSize) {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 2;
        }
        Result result = new Result();
        Page attendancePage = attendanceService.getAttendancePage(pageNo, pageSize);
        result.setObj(attendancePage);
        result.setMessage(new Message(200, "成功"));
        return result;

    }

    /**
     * 通过id查询
     *
     * @param attendanceId
     * @return
     */
    @RequiresRoles(value = "manager")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/get_attendanceById")
    public Result getAttendanceById(Integer attendanceId) {

        Result result = new Result();
        Attendance attendanceByAttendanceId = attendanceService.getAttendanceByAttendanceId(attendanceId);
        result.setObj(attendanceByAttendanceId);
        result.setMessage(new Message());
        return result;
    }

}
